<template>
    <div>
        <el-dialog :title="bannerstatus.isAdd ? '添加轮播图' : '编辑轮播图'" center :visible.sync="bannerstatus.isShow"
            :before-close="cancel">
            <el-form :model="bannerForm" :rules="rules" ref="bannerRef">
                <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
                    <el-input v-model="bannerForm.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload action="#" list-type="picture-card" :limit="1" :auto-upload="false" :on-change="changeImg"
                        :on-preview="onPreview" :file-list="filelist">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="bannerForm.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                        :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click='cancel'>取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import {banneradd,banneredit,bannerinfo} from '../../request/api'
export default {
    props: ['bannerstatus'],
    data() {
        return {
            formLabelWidth: '120px',
            bannerForm: {
                title: "",//轮播图名称
                img: "",//图片(文件)
                status: 1,//状态  1正常2禁用
            },
            dialogImageUrl: '',
            dialogVisible: false,
            filelist: [],
            rules: {
                title: [
                    { required: true, message: '请输入活动名称', trigger: 'blur' },
                    { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        // 重置表单
        reset(){
            this.bannerForm={
                title: "",//轮播图名称
                img: "",//图片(文件)
                status: 1,//状态  1正常2禁用
            },
            // 清除规则
            this.$refs.bannerRef.resetFields()
            // 清除图片
            this.filelist = []
        },
        // 图片改变触发
        changeImg(file, fileList) {
            console.log(file, 'file');
            console.log(fileList, 'fileList');
            this.bannerForm.img = file.raw
        },
        onPreview(file) {
            this.dialogVisible = true
            this.dialogImageUrl = file.url
        },
        // 数据回显
        lookup(id){
            bannerinfo({id})
            .then(res=>{
                if(res.code==200){
                    this.bannerForm = res.list
                    this.filelist =[{url:this.bannerForm.img} ]
                    this.bannerForm.id =id
                }
            })
        },

        confirm(){
            this.$refs.bannerRef.validate((val)=>{
                if(val){
                    if(this.bannerstatus.isAdd){//添加
                        banneradd(this.bannerForm)
                        .then(res=>{
                            if(res.code==200){
                                this.$message.success(res.msg)
                                this.cancel()
                                this.$store.dispatch('banner/GetBannerInfo')
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }else{//编辑
                        console.log(this.bannerForm);
                        banneredit(this.bannerForm)
                        .then(res=>{
                            if(res.code==200){
                                this.$message.success(res.msg)
                                this.cancel()
                                this.$store.dispatch('banner/GetBannerInfo')
                            }else{
                                this.$message.error(res.msg)
                            }
                        })
                    }
                }
            })
        }
    },
    mounted() { },
    watch: {},
    computed: {},
    filters: {},
    components: {}
}
</script>
<style scoped>

</style>